import invoicepositionApi from '../../../../../public/js/common/api/invoiceposition/invoiceposition.api.js';
let _vm
const _isNothing = `<div class="weui-loadmore weui-loadmore_line"><span class="weui-loadmore__tips">暂无数据</span></div>`;
(function (window, document, $) {
    $(function () {
        _vm = new Vue({
            el: "#invoicePosition",
            data() {
                return {
                    queryKey: '',
                    map: '',
                    inpositionlist: [],
                    flag: true
                }
            },
            onLond() {
                this.$nextTick(() => {
                    let _this = this;
                    _this.map = new AMap.Map('container', {
                        zoom: 15, //缩放级别
                        resizeEnable: true, //自动定位
                    });
                })
            },
            methods: {
                showMap(e) {
                    let lng = $(e.currentTarget).attr('data-lng')
                    let lat = $(e.currentTarget).attr('data-lat')
                    let address = $(e.currentTarget).attr('data-address')
                    let companyName = $(e.currentTarget).find('.feedbacktype').text()
                    let position = '';
                    let mapSize = 20;
                    let infoWindow = new AMap.InfoWindow({ offset: new AMap.Pixel(10, -30) });
                    let mapZoom = 15;
                    position = [lng, lat];
                    let map = new AMap.Map("mapchars", {
                        zoom: mapZoom, //初始地图级别
                        //mapStyle: 'amap://styles/55e15d295a532adfafab18486d16c64d', //设置地图的显示样式
                        mapStyle: 'amap://styles/cb27f1574eebad2cb30e7a0aed104b85',
                        center: position,
                        resizeEnable: true
                    });
                    const markerContent =
                        `<div class="custom-content-marker">
            <img class="markerImg" src = "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png">
            <div class="markerText markerTextHide">
            <div style="font-size:16px;font-weight:bold">${companyName}</div>
            <div>${address}</div>
            </div>`;
                    let marker = new AMap.Marker({
                        size: new AMap.Size(mapSize, mapSize),  //图标大小
                        imageSize: new AMap.Size(mapSize, mapSize),
                        content: markerContent
                    });
                    marker.setMap(map); //显示定位的图标
                    infoWindow.open(map, marker.getPosition());
                },
                // 返回主页
                returnHome() {
                    location.href = '../../main.html'
                },
                // 搜索列表
                search() {
                    getListDate().then(res => {
                        _vm.flag = false
                        this.$nextTick(() => {
                            $('#cells-transition').children().eq(0).find('.weui-cell-none-border').find('.weui-cell').trigger("click");
                        })
                    })
                },
                clearSearch() {
                    this.queryKey = ""
                    _vm.flag = false
                    getListDate().then(res => {
                        this.$nextTick(() => {
                            $('#cells-transition').children().eq(0).find('.weui-cell-none-border').find('.weui-cell').trigger("click");
                        })
                    })
                }
            },
            created() {
                initCommon()
                getListDate().then(res => {
                    this.$nextTick(() => {
                        $('#cells-transition').children().eq(0).find('.weui-cell-none-border').find('.weui-cell').trigger("click");
                    })
                })

            }
        });
        $('body').attr('data-weui-theme', 'light'); // 强制光明模式

    });
    // 获取数据
    const getListDate = async () => {
        let invoiceInfosList = await invoicepositionApi.getInvoiceInfos()
        _vm.inpositionlist = invoiceInfosList.filter(item => {
            return item.companyName.indexOf(_vm.queryKey) !== -1
        })
        if (!Utils.IsNotNull(_vm.inpositionlist)) {
            // $('#cells').empty().append(_isNothing);
            $('#map-container').css('display', 'none')
        }
        else {
            $('#map-container').css('display', 'block')
        }
    }
    // 初始化操作
    const initCommon = () => {
        // 设置滚动
        $(document).on('click', '#cells-transition .report-contents-item-box', function () {
            let top = $(this).index() * 55
            $("#cells-transition").stop().animate({
                scrollTop: top
            }, 500);
        });
        //TODO : 手风琴
        $(document).on('click', '#cells-transition .weui-cell-none-border', async function (e) {
            let _height = $(this).next().find('div').height();
            if ($(this).find("i").is('.icon-minus-line') && _vm.flag == true) {
                $(this).find("i").removeClass('icon-minus-line').addClass('icon-add-line');
                $(this).next().css({ maxHeight: `0px` });
                _vm.flag == false
            } else {
                let imageId = $(this).data('imageid')
                if (Utils.IsNotNull(imageId)) {
                    invoicepositionApi.requestImagePath(imageId).then(res => {
                        $(this).siblings('.catelist').find('.images').attr('src', res)
                        $(this).siblings('.catelist').find('.images').css('display', 'block')
                    }, err => {
                        $(this).siblings('.catelist').find('.images').css('display', 'none')
                    })
                }
                else {
                    $(this).siblings('.catelist').find('.images').css('display', 'none')
                }

                $(this).find("i").removeClass('icon-add-line').addClass('icon-minus-line');
                $(this).next().css({ maxHeight: `${_height + 24}px` });
                $(this).parent('.report-contents-item-box').siblings().find('.element').css({ maxHeight: `0px` });
                $(this).parent('.report-contents-item-box').siblings().find("i").removeClass('icon-minus-line').addClass('icon-add-line');
                _vm.flag = true

            }
        });

        // 地址提示信息
        $(document).on('click', '.markerImg', function () {
            if ($(this).siblings('.markerText').is('.markerTextHide')) {
                $(this).siblings('.markerText').removeClass('markerTextHide').addClass('markerTextShow')
            }
            else {
                $(this).siblings('.markerText').removeClass('markerTextShow').addClass('markerTextHide')
            }
        })

    }
})(window, document, jQuery)
